package com.gitee.mp.core.interceptor;

import cn.hutool.core.collection.CollUtil;
import com.gitee.mp.domain.condition.ConditionBuilder;
import com.gitee.mp.domain.condition.ConditionResult;

import com.gitee.mp.infra.constants.MilvusPlusConstants;

import java.lang.reflect.Method;

/**
 * @author hongda.li
 */
public class SelectInterceptor extends AbstractMapperInterceptor {

    @Override
    public boolean supported(Method method) {
        return MilvusPlusConstants.MethodName.METHOD_SELECT.equals(method.getName());
    }

    @Override
    public void intercept(InterceptorContext context) {
        ConditionBuilder<?> builder = (ConditionBuilder<?>) CollUtil.getFirst(context.getArguments());
        ConditionResult condition = builder.build(context.getDefinition().getFieldDefinitionList());

        if (condition.isSearch()) {
            context.setResult(context.getConnector()
                    .search(this.getCollectionName(context),
                            condition.getVectorList(),
                            condition.getExpression(),
                            condition.getLimit(),
                            condition.getOffset(),
                            condition.getOutputFields()));
        } else {
            context.setResult(context.getConnector()
                    .query(this.getCollectionName(context),
                            condition.getExpression(),
                            null,
                            condition.getLimit(),
                            condition.getOffset(),
                            condition.getOutputFields()));
        }
    }

}
